package com.henghai.zhenxuan.ui.page.home;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.bean.WinnerBean;
import com.henghai.zhenxuan.domain.request.WinningRequester;
import com.henghai.zhenxuan.ui.page.adapter.WinnerListAdapter;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;

import java.util.ArrayList;
import java.util.List;

public class NoticeFragment extends BaseFragment {
    private NoticeStates mStates;

    private WinningRequester mWinningRequester;
    private WinnerListAdapter mAdapter;

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(NoticeStates.class);
        mWinningRequester = getFragmentScopeViewModel(WinningRequester.class);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mWinningRequester.getResult().observe(this, dataResult -> {
            if (!dataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(dataResult.getResponseStatus().getResponseCode());
                return;
            }
            mStates.winnerList.set(dataResult.getResult().getData().getRows());
        });
        mWinningRequester.request(this);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new WinnerListAdapter(getContext());
        mAdapter.setOnItemClickListener((i, bean, i1) -> {
//              nav().navigate(R.id.action_noticeFragment_to_NoticeDetailFragment);
        });
        return new DataBindingConfig(com.henghai.zhenxuan.R.layout.fragment_notice, BR.vm, mStates).addBindingParam(BR.listener, new ListenerHandler()).addBindingParam(BR.adapter, mAdapter);
    }

    public class ListenerHandler implements OnTitleBarListener {
        @Override
        public void onLeftClick(TitleBar titleBar) {
            OnTitleBarListener.super.onLeftClick(titleBar);
            nav().navigateUp();
        }

        @Override
        public void onRightClick(TitleBar titleBar) {
            OnTitleBarListener.super.onRightClick(titleBar);
        }
    }

    public static class NoticeStates extends StateHolder {
        public final State<List<WinnerBean.RowsBean>> winnerList = new State<>(new ArrayList<>());
    }
}
